package com.daoImpl;

import com.bean.Category;
import com.dao.BasicDAO;
import com.dao.CategoryDAO;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author 徐洲
 * @version 1.0
 */
public class CategoryDAOImpl extends BasicDAO<Category> implements CategoryDAO {
    @Override
    public List<Category> getCategoryName() {
        String sql = "SELECT * FROM category";
        return queryMulti(sql, Category.class);
    }

    @Test
    public void test() {
        String sql = "SELECT * FROM category";
        System.out.println(queryMulti(sql, Category.class));
    }

    @Override
    public boolean addCategory(String category_name) {
        String sql = "INSERT INTO category VALUES (NULL, ?)";
        Integer integer = update(sql, category_name);
        if (integer == null) return false;
        return integer > 0;
    }

    @Override
    public boolean updateCategory(String category_name, String category_id) {
        String sql = "UPDATE category SET category_name = ? WHERE category_id = ?";
        Integer integer = update(sql, category_name, category_id);
        if (integer == null) return false;
        return integer > 0;
    }

    @Override
    public boolean deleteCategory(String category_id) {
        String[] sqls = {"DELETE FROM collect WHERE article_id IN (SELECT article_id FROM article WHERE category_id = ?)",
                "DELETE FROM `comment` WHERE article_id IN (SELECT article_id FROM article WHERE category_id = ?)",
                "DELETE FROM article WHERE category_id = ?",
                "DELETE FROM category WHERE category_id = ?"};

        return transactionUpdate(sqls, category_id);

    }
}
